package com.common.project.util;

import com.common.project.table.User;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AutoCreateCode
 * @Description
 * @Author jiangshicun@bicai365.com
 * @Date 2019/6/17 20:51
 */
public class AutoCreateCode {
    /**
     * @param poClass    javaBean
     * @param outputPath 输出目录 eg:D:不带//
     */
    public static void createVOAndTransfer(Class poClass, String outputPath) {

        // 拼装数据
        Map<String, Object> formBeanMap = new HashMap<String, Object>();

        // 类名称
        formBeanMap.put("beanName", poClass.getSimpleName());

        // 获取属性
        Field[] declaredFields = poClass.getDeclaredFields();

        // 用于封装属性
        List<Map<String, String>> paramsList = new ArrayList<Map<String, String>>();

        for (int i = 0; i < declaredFields.length; i++) {
            Map<String, String> tmpParamMap = new HashMap<String, String>();

            //排除 serialVersionUID 和 isDeleted字段
            if (!declaredFields[i].getName().equals("serialVersionUID") && !declaredFields[i].getName().equals("isDeleted")) {
                // 参数类型
                tmpParamMap.put("paramType", declaredFields[i].getType().getSimpleName());

                // 参数名称
                tmpParamMap.put("paramName", declaredFields[i].getName());

                paramsList.add(tmpParamMap);
            }
        }

        formBeanMap.put("params", paramsList);

        createTemplate(formBeanMap, outputPath, "VO.java.ftl", "VO.java");

        createTemplate(formBeanMap, outputPath, "VOTransfer.java.ftl", "Transfer.java");

    }

    private static void createTemplate(Map<String, Object> formBeanMap,
                                       String outputPath,
                                       String templatePath,
                                       String javaFileName) {
        try {
            freemarker.template.Configuration config = new freemarker.template.Configuration();
            config.setClassForTemplateLoading(AutoCreateCode.class, "/templates");
            config.setObjectWrapper(new DefaultObjectWrapper());
            Template formBeanTemplate = config.getTemplate(templatePath, "UTF-8");
            Writer out = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(outputPath + "\\" + formBeanMap.get("beanName") + javaFileName),
                            "UTF-8"));
            formBeanTemplate.process(formBeanMap, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        createVOAndTransfer(User.class,"D:\\works\\src\\main\\java\\com\\common\\project\\voconverter");
    }
}
